import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:s_mankey/config/net/smonkey_ui_api.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/storage_manager.dart';
import 'package:s_mankey/model/user_model.dart';
import 'package:s_mankey/provider/provider_widget.dart';
import 'package:s_mankey/ui/page/homeWidget//home_sort.dart';
import 'package:s_mankey/ui/page/homeWidget//home_swiper.dart';
import 'package:s_mankey/ui/page/homeWidget/enterprise.dart';
import 'package:s_mankey/ui/page/homeWidget/home_swiper_demo.dart';
import 'package:s_mankey/view_model/home_vmodel.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:s_mankey/view_model/login_vmodel.dart';
import 'homeWidget//home_appbar.dart';
import 'homeWidget//hot_shared.dart';
import 'homeWidget//articleList.dart';
import 'homeWidget/home_audio.dart';
import 'moreWidget/select_city.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePage createState() => _HomePage();
}

class _HomePage extends State<HomePage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {

    Future.delayed(Duration(milliseconds: 200), (){
      var data = StorageManager.localeStorage.getItem(AppGlobalConfig.USERINFO);
      if(data != null) {
        var model = Provider.of<LoginVModel>(context,listen: false);
        UserModel userModel = UserModel.fromJson(data);
        http.options.headers = {'token': userModel.accessToken};
        model.saveUser(userModel);
        debugPrint('首页的toekn${userModel.accessToken}');
      }
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: HomeAppBar(), // 搜索栏
        body: ProviderWidget<HomeDataVModel>(
            model: HomeDataVModel(),
            onModelReady: (model) => model.initData(),
            builder: (ctx, model, child) {
//              var loginModel = Provider.of<LoginVModel>(context);
              return model.showLoading
                  ? LoadingWidget()
                  : ListView(
                      children: <Widget>[
                        HomeSwiper(datas: model.banner), //轮播图
//          HomeSort(), // 顶部功能分类
                        HomeAudio(datas: model.audio,), //音频
                        HotShared(datas: model.ariticle,), // 文章分享
                        Enterprise(datas: model.enterprise,), // 企业
          ArticleList(datas: model.course,), //课程分类
                      ],
                    );
            }));
  }
}
